package com.cjhstudy.mall.controller;

import com.cjhstudy.mall.consts.MallConst;
import com.cjhstudy.mall.form.OrderCreateForm;
import com.cjhstudy.mall.pojo.User;
import com.cjhstudy.mall.service.IOrderService;
import com.cjhstudy.mall.vo.OrderVo;
import com.cjhstudy.mall.vo.ResponseVo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

/**
 * @author: phil
 * @ProjectName: mall
 * @Date: 2021/12/11
 */
@RestController
public class OrderController {

    @Autowired
    private IOrderService orderService;

    /**
     * 创建订单
     * @param form 校验表单
     * @param session 用户登录的session
     * @return
     */
    @PostMapping("/orders")
    public ResponseVo<OrderVo> create(@Valid @RequestBody OrderCreateForm form, HttpSession session){
        User user = (User) session.getAttribute(MallConst.CURRENT_USER);
        return orderService.create(user.getId(), form.getShippingId());
    }

    /**
     * 获取购物车列表
     * @param pageNum 分页数量
     * @param pageSize 分页的大小
     * @param session  用户登录的session
     * @return
     */
    @GetMapping("/orders")
    public ResponseVo<PageInfo> list(@RequestParam(required = false,defaultValue = "1") Integer pageNum,
                                     @RequestParam(required = false,defaultValue = "10") Integer pageSize,
                                     HttpSession session){
        User user = (User) session.getAttribute(MallConst.CURRENT_USER);
        return orderService.list(user.getId(),pageNum,pageSize);
    }


    /**
     * 查询订单的详情
     * @param orderNo 订单号
     * @param session 用户登录的session
     * @return
     */
    @GetMapping("/orders/{orderNo}")
    public ResponseVo<OrderVo> detail(@PathVariable Long orderNo,HttpSession session){
        User user = (User) session.getAttribute(MallConst.CURRENT_USER);
        return orderService.detail(user.getId(), orderNo);
    }


    /**
     * 用户取消订单
     * @param orderNo 订单号
     * @param session 用户登录的session
     * @return
     */
    @PutMapping("/order/{orderNo}")
    public ResponseVo cancel(@PathVariable Long orderNo,HttpSession session){
        User user = (User) session.getAttribute(MallConst.CURRENT_USER);
        return orderService.cancel(user.getId(), orderNo);
    }

}
